package com.example.myspringpro.dao;

import com.example.myspringpro.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @Description
 * @Author allenhu <huxiangneng@163.com>
 * @Date 2019/04/30
 * @Version V1.0.0
 **/
@Mapper
public interface UserDao {

    /**
     * 通过名字查询用户信息
     */
    @Select("SELECT * FROM user WHERE user_name = #{userName} limit 1")
    @Results(id = "userMap", value = {
            @Result(property = "userName", column = "user_name", javaType = String.class)
    })
    User findUserByName(@Param("userName") String userName);

    /**
     * 查询所有用户信息
     */
    @Select("SELECT * FROM user")
    @ResultMap("userMap")
    List<User> findAllUser();

    /**
     * 插入用户信息
     */
    @Insert("INSERT INTO user(user_name, password, email) VALUES(#{userName}, #{password}, #{email})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insertUser(User user);

    /**
     * 根据 id 更新用户信息
     */
    @Update("UPDATE  user SET name = #{name},age = #{age},money= #{money} WHERE id = #{id}")
    void updateUser(@Param("name") String name, @Param("age") Integer age, @Param("money") Double money,
                    @Param("id") int id);

    /**
     * 根据 id 删除用户信息
     */
    @Delete("DELETE from user WHERE id = #{id}")
    void deleteUser(@Param("id") int id);
}
